package com.zhmsky.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author zhmsky
 * @date 2021/12/2 17:30
 */
@Controller
public class LoginController {

    @GetMapping("/login")
    public String login(String username, String password, Model model) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的表单信息
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);   //信息匹配则跳转主页
            return "index";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg","用户名不存在");
            return "/login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "/login";
        }
    }
}
